<?php
$apikey = '';
$daysahead = 7;
$to_addr = '';
$from_addr = '';

if( !function_exists('json_decode') ) {
  function json_decode($x) {
    require_once('JSON.php');
    $json = new Services_JSON;
    return $json->decode($x);
  }
}

function fetch($url) {
    return json_decode(file_get_contents($url));    
}

//set up array to hold items
$days = array();
$days['Overdue'] = array();
for($i = 0; $i < $daysahead; $i++) {
    $days[date('l, M j',strtotime("+$i day"))] = array();
}

//only get items with due date before cutoff
$dt_cut = strtotime("+$daysahead day");

//define api strings
$tok = 'token=' . $apikey;
$cmds = array();
$cmds['projects'] = 'http://todoist.com/API/getProjects?';
$cmds['items'] = 'http://todoist.com/API/getUncompletedItems?';

//fill arrayy with items
$projs = fetch($cmds['projects'] . $tok);
foreach($projs as $prj) {
    $items = array();
    $tasks = fetch($cmds['items'] . 'project_id=' . $prj->id .'&' . $tok);
    foreach($tasks as $tsk) {
        if($tsk->due_date != '') {
            $dt = strtotime($tsk->due_date);
            $cnt = $tsk->content . 
	      ' <span style="background-color:' . $prj->color. ';">' . 
              '[' . $prj->name . ']</span>';
            if($dt <= strtotime('yesterday')) {
                $days['Overdue'][] = $cnt;
            } elseif($dt <= $dt_cut) {
                $days[date('l, M j', $dt)][] = $cnt;   
            }
        }       
    }
}

//format for output
$i = 0;

$ret ='<div style="font-family: helvetica,sans-serif;">';
$h3 = '<h3 style="margin-top:5px; margin-bottom:5px;">';

foreach($days as $key => $val) {
  if($i != 0) { $dt = strtotime($key); }
  $cl = (count($val) > 0) ? '#202020' : '#AAAAAA';

  //create heading html
  if($i == 0) {$dt_hd = '<font color=#FF0000>Overdue</font>'; $dt_str = ''; } 
  elseif($i == 1) {$dt_hd = 'Today'; $dt_str = date('D M j', $dt); } 
  elseif($i == 2) {$dt_hd = 'Tomorrow'; $dt_str = date('D M j', $dt); } 
  else {$dt_hd = date('l', $dt); $dt_str = date('M j', $dt); } 
  $hd = '<h3 style="margin-top:5px; margin-bottom:5px;">' .
        '<span style="font-size:17px;">' . $dt_hd . '</span> ' .
        '<span style="font-size:11px;">' . $dt_str . '</span>' .
        '</h3>';

  //write html
  $ret .= '<span style="color:' . $cl . '; ">';
  //create the list for each day
  //omit Overdue section unless it has items
  if(count($val) > 0 || $i != 0) {
    $ret .= $hd;
    $ret .= '<ul style="border-bottom: 1px solid #CCCCCC; margin:0px; padding-bottom:5px; padding-left:10px;">';
    if(count($val) > 0) {
      foreach ($val as $item) {
	$ret .= "<li>" . $item . "</li>";
      }
    }
    $ret .= '</ul>';
  } 
  $ret .= '</span>';
  $i++;
}
$ret .= '</div>';

//print $ret;
$headers = 'From: Todoist <' . $from_addr . '> ' . "\n" .
           'Content-Type: text/html; charset=UTF-8' . "\n" .
           'MIME-Version: 1.0' . "\n";
mail($to_addr, 'Tasks for ' . date('l M j') , $ret, $headers);
?>